package cdu.javaendwork.Controller.Shopping;

import cdu.javaendwork.model.sqlModel.Goods;
import cdu.javaendwork.model.sqlModel.Items;
import cdu.javaendwork.model.sqlModel.Order;
import cdu.javaendwork.model.sqlModel.User;
import cdu.javaendwork.service.Impl.Shopping.GoodsServiceImpl;
import cdu.javaendwork.service.Impl.Shopping.ItemsServiceImpl;
import cdu.javaendwork.service.Impl.Shopping.OrderServiceImpl;
import cdu.javaendwork.service.Interface.Shopping.GoodsService;
import cdu.javaendwork.service.Interface.Shopping.ItemsService;
import cdu.javaendwork.service.Interface.Shopping.OrderService;
import cdu.javaendwork.util.pageHelp.PageHelper;
import cdu.javaendwork.util.pageHelp.PageInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//查看购物车
@WebServlet("/craft")
public class CraftController extends HttpServlet {
    //声明需要提供服务的service以及分页工具
    OrderService orderService = new OrderServiceImpl();
    GoodsService goodsService = new GoodsServiceImpl();
    ItemsService itemsService = new ItemsServiceImpl();
    PageHelper pageHelper = new PageHelper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //拿到页码
        String sPage=req.getParameter("page");
        int pageSize=3;
        int pageIndex=(sPage == null || sPage.equals("")) ? 1 : Integer.parseInt(sPage);

        //拿到登录用户的id
        HttpSession session = req.getSession();
        User user= (User)session.getAttribute("user");
        int userid = user.getId();

        //寻找一下该用户是否有存在的购物车
        Order oldCraft = orderService.findByUserIdAndStatus(userid,1);

        //如果没有则创建一个新的购物车
        if(oldCraft == null) orderService.insert(userid ,1,new Timestamp(new Date().getTime()),new BigDecimal(0)," ");
        //找到该用户的购物车id
        Order realCraft = orderService.findByUserIdAndStatus(userid , 1);
        int craftId = realCraft.getId();
        //分页查询该购物车中的条目
        List<Items> itemsList = itemsService.filterByOrderIdLimit(craftId,pageIndex,pageSize);
        //查询该集合的分页信息
        PageInfo pageInfo = pageHelper.filter("Items","orderid="+craftId,pageIndex,pageSize);

        //获取每个条目对应的商品信息
        List<Goods> goodsList = new ArrayList<>();
        for(Items items:itemsList)
        {
            int goodid = items.getGoodid();
            Goods goods = goodsService.findById(goodid);
            goodsList.add(goods);
        }
        //将得到的信息丢到页面去
        req.setAttribute("craft",realCraft);
        req.setAttribute("goodsList",goodsList);
        req.setAttribute("itemsList",itemsList);
        req.setAttribute("pageInfo",pageInfo);

        req.getRequestDispatcher("craft.jsp").forward(req,resp);
    }
}
